//
//  PGNewFeatureViewController.swift
//  PGWeibo
//
//  Created by Apple on 15/11/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit

private let NewFeatureReuseCellId = "NewFeatureReuseCell"
let NewFeatureImageCount = 4
class PGNewFeatureViewController: UICollectionViewController {
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: NewFeatureReuseCellId)

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewFeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureReuseCellId, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let currentPage = scrollView.contentOffset.x / scrollView.bounds.width
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: Int(currentPage), inSection: 0)) as! NewFeatureCell
        //第四页 播放动画
        if currentPage + 1 == 4 {
            
            cell.setAnim()
        }
    }
}

private class NewFeatureCell: UICollectionViewCell {
    private var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            enterMain.alpha = 0
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(iconView)
        addSubview(enterMain)
        enterMain.alpha = 0
        enterMain.addTarget(self, action: "enter", forControlEvents: .TouchUpInside)
        //自动布局
        iconView.frame = bounds
        enterMain.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
    }
    /// 进入微博
    @objc private func enter() {
        NSNotificationCenter.defaultCenter().postNotificationName(PGSwitchRootControllerNotification, object: nil)
    }
    //动画
    private func setAnim() {
        enterMain.alpha = 1
        enterMain.transform = CGAffineTransformMakeScale(0, 0)
        enterMain.userInteractionEnabled = false
        //initialSpringVelocity  初始速度 模拟重力加速度
        //usingSpringWithDamping 弹力系数  越小越弹
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.enterMain.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                self.enterMain.userInteractionEnabled = true
        }
    }
    
    private lazy var iconView: UIImageView = UIImageView()
    private lazy var enterMain: UIButton = UIButton(iconImage: nil, BackImage: "new_feature_finish_button", title: "进入微博", textColor: UIColor.whiteColor())
}
